package com.lxyk.springcloud.controller;

import com.lxyk.springcloud.pojo.Payment;
import com.lxyk.springcloud.pojo.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @ClassName OrderController
 * @Description TODO
 * @Author FJQ
 * @Date 2022/11/14 22:01
 * @Version 1.0
 **/
@RestController
@Slf4j
public class OrderController {

    //实现轮询：当访问http://localhost/consumer/payment/get/1 从消费者调提供者时 这里不能写成固定的8001端口 而要写服务的名字
    // 这个名字下有两个端口 分别是8001和8002 这样访问http://localhost/consumer/payment/get/1才能实现轮询算法 否则就是一直是8001
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";/*"http://localhost:8001";*/

    @Autowired
    private RestTemplate restTemplate;

    @PostMapping(value = "/consumer/payment/create")
    public ResultInfo<Payment> create( Payment payment){
       return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,ResultInfo.class);
    }

    @GetMapping(value = "/consumer/payment/get/{id}")
    public ResultInfo<Payment> getPaymentById(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResultInfo.class);
    }

    @GetMapping(value = "/consumer/payment/zipkin")
    public String paymentzipkin(){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/zipkin",String.class);
    }
}
